Imports Microsoft.VisualBasic
Imports System.Diagnostics
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Data
Imports System.Xml.XPath
Imports Excel
Imports System.Web.HttpContext

<Serializable()> _
Public Class PrivateEngine

    Private app_data As String

    Private _simId As Integer
    'Private Shared snapShot As Hashtable

    Private _snapShotData As Collections.SortedList

    Private _selectedTrainingsCol As Collection

    Private _selectedInjuriesCol As Collection

    Private _selectedPlayersCol As Collection

    Private _simulation As Base

    ' season -> (playerId -> count)
    Private uniquePlayerCount As Hashtable '# of times a player gets injured every season

    ' season -> (injuryId -> count)
    Private _uniqueInjuryCount As Hashtable

    Private _retireesCount As Hashtable


    Public Property SnapShotData() As Collections.SortedList
        Get
            Return _snapShotData
        End Get
        Set(ByVal value As Collections.SortedList)
            _snapShotData = value
        End Set
    End Property


    Public Property UniqueInjuryCount() As Hashtable
        Get
            Return _uniqueInjuryCount
        End Get
        Set(ByVal value As Hashtable)
            _uniqueInjuryCount = value
        End Set
    End Property


    Public Property RetireesCount() As Hashtable
        Get
            Return _retireesCount
        End Get
        Set(ByVal value As Hashtable)
            _retireesCount = value
        End Set
    End Property


    Public Property AppData() As String
        Get
            Return app_data
        End Get
        Set(ByVal value As String)
            app_data = value
        End Set

    End Property

    Public Property SimID() As Integer
        Set(ByVal value As Integer)
            _simId = value
        End Set
        Get
            Return _simId
        End Get
    End Property

    Public Property SelectedTrainingsCol() As Collection
        Set(ByVal value As Collection)
            _selectedTrainingsCol = value
        End Set
        Get
            Return _selectedTrainingsCol
        End Get
    End Property

    Public Property SelectedInjuriesCol() As Collection
        Set(ByVal value As Collection)
            _selectedInjuriesCol = value
        End Set
        Get
            Return _selectedInjuriesCol
        End Get
    End Property

    Public Property SelectedPlayersCol() As Collection
        Set(ByVal value As Collection)
            _selectedPlayersCol = value
        End Set
        Get
            Return _selectedPlayersCol
        End Get
    End Property

    Public Property Simulation() As Base
        Set(ByVal value As Base)
            _simulation = value
        End Set
        Get
            Return _simulation
        End Get
    End Property



    Public Function Start() As Boolean

        uniquePlayerCount = New Hashtable
        _uniqueInjuryCount = New Hashtable
        _retireesCount = New Hashtable

        Dim calc_start As DateTime = DateTime.Now


        _simulation = DBConnection.FET_SIMULATION(_simId)
        Dim trainingMap As Hashtable = CType(_simulation.Setup, SimulationSetup).TrainingMap

        'insert an entry with status of 'started'
        Dim rId As Integer = DBConnection.INS_NEW_REPORT_ENTRY(_simId)
        Dim completed As Boolean = True

        Try

            'selectedPlayersCol list of selected players
            'selectedInjuriesCol list of injuries being used
            'selectedTrainingsCol list of trainings being used.
            'use the above objects instead of going to the database everytime
            'snapShot = New Hashtable
            'lookupsCol = New Collections.SortedList
            _snapShotData = New Collections.SortedList
            Dim cnt As Integer = 0
            Dim training As Base
            For season As Integer = 1 To Cons.MAX_SEASONS
                Dim trainingKey As String = "Season" & season
                'Debug.WriteLine("Season" & season & " - " & trainingMap.Item(trainingKey))

                training = Utility.GetBase(_selectedTrainingsCol, trainingMap.Item(trainingKey))
                For week As Integer = 1 To Cons.MAX_WEEKS
                    'start of week
                    'Debug.WriteLine("Week" & week & " - ")

                    For Each player As Base In _selectedPlayersCol
                        Dim _key As Lookup = Utility.GetKey(season, week, player)
                        'lookupsCol.Add(_key, _key)

                        Dim player_setup As PlayerSetup = CType(player.Setup, PlayerSetup)
                        Dim training_setup As TrainingSetup = CType(training.Setup, TrainingSetup)

                        player_setup.UpdateInjuryStatus()

                        'if player is already retired, do recount him
                        If player_setup.Status.Equals(Cons.PLAYER_STATUS_RETIRED) Then
                            _snapShotData.Add(_key, player.Clone())
                            cnt += 1

                            Continue For

                        End If

                        'only count as retired during the season he retires
                        If player_setup.AttemptToRetire Then
                            'just retired

                            If _retireesCount(season) Is Nothing Then
                                _retireesCount.Add(season, 1)
                            Else
                                Dim num As Integer = _retireesCount.Item(season)
                                _retireesCount.Remove(season)
                                _retireesCount.Add(season, num + 1)
                            End If

                            _snapShotData.Add(_key, player.Clone())
                            cnt += 1

                            Continue For
                        End If

                        'if injured, reduce recoveryleft by 1week,
                        If player_setup.IsInjured() Then

                            player_setup.RecoveryLeft -= 1

                        Else
                            'else not injury
                            Dim isInjuryOn As Boolean = True

                            If _selectedInjuriesCol.Count > 0 Then
                                isInjuryOn = True
                            End If

                            Dim injured As Boolean = False

                            'there are injuries selected so account for them
                            If (isInjuryOn) Then

                                Dim injury As Base = Calcs.CalcInjuryType(_simulation, player, _selectedInjuriesCol)

                                If (Not injury Is Nothing) Then
                                    injured = True
                                    'injury is on and he got injured


                                    player_setup.RecoveryLeft = Calcs.CalcRecoveryPeriod(player, injury)
                                    player_setup.InjuryType = injury.Name


                                    'count the number of times each player gets injured
                                    If (uniquePlayerCount(season) Is Nothing) Then
                                        uniquePlayerCount.Add(season, New Hashtable)
                                    End If

                                    Dim tempHt As Hashtable = uniquePlayerCount(season)

                                    If (tempHt(player.ID) Is Nothing) Then
                                        'new player, new injury
                                        tempHt.Add(player.ID, 1)
                                    Else
                                        'player has been injured before
                                        Dim num As Integer = tempHt(player.ID)
                                        tempHt.Remove(player.ID)
                                        tempHt.Add(player.ID, num + 1)    'incremement the # of injuries for that player

                                    End If



                                    'count the number of players affected by an injury
                                    If (_uniqueInjuryCount(season) Is Nothing) Then
                                        _uniqueInjuryCount.Add(season, New Hashtable)
                                    End If

                                    Dim tempHtb As Hashtable = _uniqueInjuryCount(season)

                                    If (tempHtb(injury.ID) Is Nothing) Then
                                        'new injury, new guy
                                        tempHtb.Add(injury.ID, 1)
                                    Else
                                        'player has been injured before
                                        Dim num As Integer = tempHtb(injury.ID)
                                        tempHtb.Remove(injury.ID)
                                        tempHtb.Add(injury.ID, num + 1)    'incremement the # of injuries for that player

                                    End If




                                Else
                                    'injury is on and he didnt get injured
                                    injured = False
                                End If


                            End If

                            'if not injured then attempt to train
                            If (Not injured) Then

                                'no injury picked up, so give him training boost

                                'figure out his age to know how much % of the boost he gets
                                Dim ageRangeBoost As Double = Calcs.TrainingBoostBasedOnAgeRange(player, training)

                                Dim boost As Double = 0

                                If (training_setup.TrainingType = Cons.TRAINING_TYPE_Attribute) Then

                                    Dim attributeInTraining As String = CType(training.Setup, TrainingSetup).TrainingTypeSelected

                                    If (training_setup.TrainingAffecting = Cons.TRAINING_AFFECTING_Positions) Then
                                        'if affecting position

                                        Dim position As String = player_setup.Position
                                        boost = training_setup.GetPosition(position) / 100.0

                                        boost *= ageRangeBoost

                                        player_setup.Increment(attributeInTraining, boost)
                                    ElseIf (training_setup.TrainingAffecting = Cons.TRAINING_AFFECTING_Zones) Then
                                        'if affecting zones/multiple positions

                                        Dim zone As String = player_setup.GetZone()
                                        boost = training_setup.GetZone(zone) / 100.0

                                        boost *= ageRangeBoost

                                        player_setup.Increment(attributeInTraining, boost)

                                    End If

                                ElseIf (training_setup.TrainingType = Cons.TRAINING_TYPE_Category) Then
                                    'if training a category/multiple attributes

                                    'contains multiple attributes
                                    Dim categoryInTraining As String = CType(training.Setup, TrainingSetup).TrainingTypeSelected

                                    If (training_setup.TrainingAffecting = Cons.TRAINING_AFFECTING_Positions) Then
                                        'if affecting position
                                        Dim position As String = player_setup.Position
                                        boost = training_setup.GetPosition(position) / 100.0

                                        boost *= ageRangeBoost

                                        For Each attribute As String In Cons.GET_ATTRIBUTES_FOR_CATEGORY(categoryInTraining)
                                            player_setup.Increment(attribute, boost)

                                        Next

                                    ElseIf (training_setup.TrainingAffecting = Cons.TRAINING_AFFECTING_Zones) Then
                                        'if affecting zones/multiple positions

                                        Dim zone As String = player_setup.GetZone()
                                        boost = training_setup.GetZone(zone) / 100.0

                                        boost *= ageRangeBoost

                                        For Each attribute As String In Cons.GET_ATTRIBUTES_FOR_CATEGORY(categoryInTraining)
                                            player_setup.Increment(attribute, boost)
                                        Next

                                    End If


                                End If

                            End If



                        End If




                        'player.Setup = setup

                        'snapShot.Add(_key.ToString(), player.Clone())
                        _snapShotData.Add(_key, player.Clone())
                        cnt += 1
                    Next

                    'end of week
                Next
                'end of season

                'update the player ages (TODO: attempt to retire players over 35.)
                For Each player As Base In _selectedPlayersCol
                    CType(player.Setup, PlayerSetup).Age += 1
                Next

                'move from col to tempCol
                Dim tempCol As New Collection


                For c As Integer = 1 To _selectedPlayersCol.Count
                    tempCol.Add(_selectedPlayersCol.Item(c))
                    'selectedPlayersCol.Remove(c)
                Next

                SelectedPlayersCol = New Collection
                'randomize the players in selectedPlayersCol.
                While tempCol.Count > 0
                    Dim gen As Integer = Calcs.GenerateNumberRandom(tempCol.Count, tempCol.Count)
                    _selectedPlayersCol.Add(tempCol.Item(gen))
                    tempCol.Remove(gen)

                End While

            Next


            Dim calc_end As DateTime = DateTime.Now


            Debug.WriteLine("MUST BE EQUAL " & cnt & " = " & (Cons.MAX_SEASONS * Cons.MAX_WEEKS * _selectedPlayersCol.Count))
            Debug.WriteLine("Time(s) taken to calc = " & (calc_end - calc_start).TotalSeconds)

        Catch ex As Exception
            completed = False
        End Try

        If completed Then
            Dim report_start As DateTime = DateTime.Now
            ReportDataToDatabase(rId)
            Dim report_end As DateTime = DateTime.Now
            Debug.WriteLine("Time(s) taken to write report to db= " & (report_end - report_start).TotalSeconds)

        Else
            'insert entry in report table 'failed', timestamp
            DBConnection.UPD_REPORT_ENTRY_FAILED(rId)
        End If

    End Function


    Private Function ReportDataToDatabase(ByVal rId As Integer) As Boolean

        Utility.Serialize(app_data, rId, Me)

    End Function


    Private Function ReportData(ByVal rId As Integer) As Boolean

        'get the template copy from the app_data
        Dim templateCopy As String = GetTemplateCopy(app_data, rId)

        Dim app As New Application
        app.DisplayAlerts = False

        ' create the workbook object by opening the excel file.
        Dim workbook As Workbook = app.Workbooks.Open(templateCopy)

        ' get the active worksheet using sheet name or active sheet
        Dim summaryWorksheet As Worksheet = workbook.Worksheets.Item(1) 'summary
        PopulateSummaryTab(summaryWorksheet)

        Dim injuriesWorksheet As Worksheet = workbook.Worksheets.Item(2) 'injuries
        PopulateInjuriesTab(injuriesWorksheet)

        Dim trainingsWorksheet As Worksheet = workbook.Worksheets.Item(3) 'trainings
        PopulateTrainingsTab(trainingsWorksheet)

        Dim playersWorksheet As Worksheet = workbook.Worksheets.Item(4) 'players
        PopulatePlayersTab(playersWorksheet)

        'save the report in the downloads folder
        'Dim saveString As String = AppData & "Downloads\" & "sample-template-with-data-" & Now.Second & ".xls"
        'workbook.SaveAs(templateCopy)
        workbook.Save()
        workbook.Close()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook)

        app.DisplayAlerts = True
        app.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(app)

        GC.Collect()

        Dim fileData() As Byte = Utility.GetBytes(templateCopy)

        'insert entry in report table 'success', data, timestamp
        DBConnection.UPD_REPORT_ENTRY_SUCCESS(rId, fileData)

        'email the report also..
        'SendEmail(templateCopy, rId)

        'delete the report from downloads folder
        Dim fi As New IO.FileInfo(templateCopy)
        fi.Delete()
        fi = Nothing

    End Function

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
    Private Shared Function GetTemplateCopy(ByVal appData As String, ByVal rId As Integer) As String
        'get the template from the app_data
        Dim template As String = appData & "sample template.xls"

        Dim app As New Application
        app.DisplayAlerts = False

        ' create the workbook object by opening the excel file.
        Dim workbook As Workbook = app.Workbooks.Open(template)
        Dim saveString As String = appData & "Downloads\" & "sample-template-with-data-" & rId & ".xls"
        workbook.SaveAs(saveString)

        workbook.Close()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook)

        app.DisplayAlerts = True
        app.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(app)

        GC.Collect()

        Return saveString

    End Function





   

    Private Sub PopulateSummaryTab(ByVal worksheet As Worksheet)

        Dim trainingRowIndex As Integer = 4
        Dim trainingColIndex As Integer = 2

        For indx As Integer = 1 To _selectedTrainingsCol.Count '15
            Dim offset As Integer = trainingRowIndex + indx - 1
            'Dim val = CType(worksheet.Cells().Item(offset, trainingColIndex), Range).Value
            CType(worksheet.Cells().Item(offset, trainingColIndex), Range).Value = CType(_selectedTrainingsCol.Item(indx), Base).Name


        Next


        Dim injuriesRowIndex As Integer = 4
        Dim injuriesColIndex As Integer = 4

        For indx As Integer = 1 To _selectedInjuriesCol.Count '15
            Dim offset As Integer = injuriesRowIndex + indx - 1
            CType(worksheet.Cells().Item(offset, injuriesColIndex), Range).Value = CType(_selectedInjuriesCol.Item(indx), Base).Name

        Next


        Dim playersRowIndex As Integer = 4
        Dim playersColIndex As Integer = 6

        For indx As Integer = 1 To _selectedPlayersCol.Count '30
            Dim offset As Integer = playersRowIndex + indx - 1
            'Dim val = CType(worksheet.Cells().Item(offset, playersColIndex), Range).Value
            CType(worksheet.Cells().Item(offset, playersColIndex), Range).Value = CType(_selectedPlayersCol.Item(indx), Base).Name
        Next


        Dim statsRow As Integer = 28
        Dim playerStatsCol As Integer = 2
        Dim injuryStatsCol As Integer = 3
        Dim retireeStatsCol As Integer = 4

        For season As Integer = 1 To Cons.MAX_SEASONS

            If (Not uniquePlayerCount(season) Is Nothing) Then
                Dim playerCount As Integer = CType(uniquePlayerCount(season), Hashtable).Count
                CType(worksheet.Cells().Item(statsRow, playerStatsCol), Range).Value = playerCount

            End If


            If (Not _uniqueInjuryCount(season) Is Nothing) Then
                Dim injuryCount As Integer = CType(_uniqueInjuryCount(season), Hashtable).Count
                CType(worksheet.Cells().Item(statsRow, injuryStatsCol), Range).Value = injuryCount

            End If

            If (Not _retireesCount(season) Is Nothing) Then
                Dim retireeCount As Integer = _retireesCount(season)
                CType(worksheet.Cells().Item(statsRow, retireeStatsCol), Range).Value = retireeCount

            End If
            statsRow += 1
        Next

    End Sub

    Private Sub PopulateInjuriesTab(ByVal worksheet As Worksheet)

        Dim startingRowIndex As Integer = 4

        Dim startingColIndex As Integer = 0
        For Each injury As Base In _selectedInjuriesCol
            startingColIndex = 0

            Dim injuryId As Integer = injury.ID

            Dim injurySetup As InjurySetup = injury.Setup

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = injury.Name

            startingColIndex += 1 'blank column

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = injurySetup.RecoveryWeeks

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = injurySetup.RecoveryAgeMap.Item(Cons.AGERANGE1)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = injurySetup.RecoveryAgeMap.Item(Cons.AGERANGE2)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = injurySetup.RecoveryAgeMap.Item(Cons.AGERANGE3)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = injurySetup.RecoveryAgeMap.Item(Cons.AGERANGE4)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = injurySetup.RecoveryAgeMap.Item(Cons.AGERANGE5)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = injurySetup.RecoveryAgeMap.Item(Cons.AGERANGE6)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = injurySetup.RecoveryAgeMap.Item(Cons.AGERANGE7)

            startingColIndex += 1 'blank column

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = CType(CType(_simulation.Setup, SimulationSetup).InjuriesMap.Item(injuryId), Hashtable).Item(Cons.AGERANGE1)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = CType(CType(_simulation.Setup, SimulationSetup).InjuriesMap.Item(injuryId), Hashtable).Item(Cons.AGERANGE2)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = CType(CType(_simulation.Setup, SimulationSetup).InjuriesMap.Item(injuryId), Hashtable).Item(Cons.AGERANGE3)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = CType(CType(_simulation.Setup, SimulationSetup).InjuriesMap.Item(injuryId), Hashtable).Item(Cons.AGERANGE4)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = CType(CType(_simulation.Setup, SimulationSetup).InjuriesMap.Item(injuryId), Hashtable).Item(Cons.AGERANGE5)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = CType(CType(_simulation.Setup, SimulationSetup).InjuriesMap.Item(injuryId), Hashtable).Item(Cons.AGERANGE6)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = CType(CType(_simulation.Setup, SimulationSetup).InjuriesMap.Item(injuryId), Hashtable).Item(Cons.AGERANGE7)


            startingRowIndex += 2
        Next



    End Sub

    Private Sub PopulateTrainingsTab(ByVal worksheet As Worksheet)
        Dim startingRowIndex As Integer = 4

        Dim startingColIndex As Integer = 0
        For Each training As Base In _selectedTrainingsCol
            startingColIndex = 0

            Dim trainingSetup As TrainingSetup = training.Setup

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = training.Name
            'CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Interior.ColorIndex = 6

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.PointsForFUllTraining

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.TrainingAgeMap.Item(Cons.AGERANGE1)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.TrainingAgeMap.Item(Cons.AGERANGE2)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.TrainingAgeMap.Item(Cons.AGERANGE3)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.TrainingAgeMap.Item(Cons.AGERANGE4)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.TrainingAgeMap.Item(Cons.AGERANGE5)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.TrainingAgeMap.Item(Cons.AGERANGE6)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.TrainingAgeMap.Item(Cons.AGERANGE7)

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.TrainingType & "(" & trainingSetup.TrainingTypeSelected & ")"

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.TrainingAffecting

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.GK

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.SW

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.RB

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.RWB

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.RCB

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.CB

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.LCB

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.LWB

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.LB

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.RM

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.RAM

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.RCM

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.RDM

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.CM

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.CAM

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.CDM

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.LDM

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.LCM

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.LAM

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.LM

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.RW

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.RF

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.RS

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.CF

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.ST

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.LS

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.LF

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.LW

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.Keeper

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.Defense

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.Midfield

            startingColIndex += 1
            CType(worksheet.Cells().Item(startingRowIndex, startingColIndex), Range).Value = trainingSetup.Forward


            startingRowIndex += 2
        Next
    End Sub

    Private Sub PopulatePlayersTab(ByVal worksheet As Worksheet)

        Dim rowIndex As Integer = 0
        Dim colIndex As Integer = 0

        Dim idLblCol As Integer = 1
        Dim idValCol As Integer = 2

        Dim nameLblCol As Integer = 1
        Dim nameValCol As Integer = 2

        Dim posLblCol As Integer = 1
        Dim posValCol As Integer = 2

        Dim overallLblCol As Integer = 2


        For Each player As Base In _selectedPlayersCol
            colIndex = 0

            Dim setup As PlayerSetup = player.Setup
            rowIndex += 1
            CType(worksheet.Cells().Item(rowIndex, idLblCol), Range).Value = "id"
            CType(worksheet.Cells().Item(rowIndex, idValCol), Range).Value = player.ID

            rowIndex += 1
            CType(worksheet.Cells().Item(rowIndex, nameLblCol), Range).Value = "name"
            CType(worksheet.Cells().Item(rowIndex, nameValCol), Range).Value = player.Name

            rowIndex += 1
            CType(worksheet.Cells().Item(rowIndex, posLblCol), Range).Value = "pos"
            CType(worksheet.Cells().Item(rowIndex, posValCol), Range).Value = setup.Position

            'labels
            rowIndex += 2
            colIndex += 2
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Overall"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Status"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Age"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Training"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Injury Type"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Recovery Left"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Crossing"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Finishing"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Heading"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Short Pass"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Volleys"


            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Shot Power"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Jumping"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Stamina"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Strength"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Long Shot"


            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Aggression"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Tact. Aware."

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Positioning"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Vision"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Penalties"



            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Curve"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Dribbling"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Freekick"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Long Pass"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Ball Control"



            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Acceleration"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Agility"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Sprint"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Balance"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Reactions"



            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Marking"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Slide Tackle"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Stand Tackle"


            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Diving"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Handling"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "KIcking"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "One On One"

            colIndex += 1
            CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Reflexes"



            rowIndex += 1

            'data

            For season As Integer = 1 To Cons.MAX_SEASONS
                rowIndex += 1
                colIndex = 1

                CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = "Season" & season

                For week As Integer = 1 To Cons.MAX_WEEKS




                    Dim key As Lookup = Utility.GetKey(season, week, player)

                    Dim snap As Base = _snapShotData.Item(key)
                    Dim snapSetup As PlayerSetup = snap.Setup

                    rowIndex += 1
                    colIndex = 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = " Week" & week

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Overall

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Status

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Age

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = Utility.GetBase(_selectedTrainingsCol, CType(_simulation.Setup, SimulationSetup).TrainingMap.Item("Season" & season)).Name

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.InjuryType

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.RecoveryLeft


                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Crossing

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Finishing

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Heading

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Short_pass

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Volleys


                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Shot_power

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Jumping

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Stamina

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Strength

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Long_shot


                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Aggression

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Tactical_awareness

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Positioning

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Vision

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Penalties


                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Curve

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Dribbling

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Freekick

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Long_pass

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.BallControl



                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Acceleration

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Agility

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Sprint

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Balance

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Reactions



                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Marking

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Slide_tackle

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Standing_tackle


                    colIndex += 1
                    Dim r As Range = worksheet.Cells().Item(rowIndex, colIndex)
                    r.Value = snapSetup.Diving
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Diving

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Handling

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Kicking

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.One_on_one

                    colIndex += 1
                    CType(worksheet.Cells().Item(rowIndex, colIndex), Range).Value = snapSetup.Reflexes


                Next

            Next


            rowIndex += 1
            rowIndex += 1
        Next




    End Sub
#Region "tests"

    Public Sub TestStuff1()
        'what injuries 
        Dim counter As Integer = 0
        Dim probOfInjury As Integer = 50
        If (counter > 10) Then
            Return
        End If
        For i As Integer = 1 To 100
            Dim b As Boolean = Calcs.CalcProbability2(Nothing) 'player
            If b Then
                counter += 1
            Else

            End If
        Next
        'Debug.WriteLine(probOfInjury & "% chance of getting injured: " & (counter))
    End Sub


#End Region


End Class
